﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class PositionManagementS: PositionManagementSI
    {
        private readonly WmssystemContext myDbContext;

        public PositionManagementS(WmssystemContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        //岗位管理添加
        public Task<int> Add(Sys_PositionManagement posAdd)
        {
            myDbContext.Sys_PositionManagement.Add(posAdd);
            return myDbContext.SaveChangesAsync();
        }
        //岗位管理删除
        public Task<int> Del(int id)
        {
            var result = myDbContext.Sys_PositionManagement.FindAsync(id).Result;
            if (result != null)
            {
                myDbContext.Sys_PositionManagement.Remove(result);
                return myDbContext.SaveChangesAsync();
            }
            else
            {
                return Task.FromResult(0);
            }
        }

        //岗位管理分页模糊查询
        public Task<List<Sys_PositionManagement>> FenYe(int pageSize, int currentPage, string Key = null, string Status = null)
        {
            var query = myDbContext.Sys_PositionManagement.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(c => c.P_MPositionName.Contains(Key) || c.P_MPositionCode.Contains(Key));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.P_MStatus.Contains(Status));
            }
            query = query.Where(c => c.P_IsDelete == true);
            // 查询
            //return query.ToListAsync();
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //岗位管理修改
        public Task<int> Upd(Sys_PositionManagement posUpd)
        {
            myDbContext.Sys_PositionManagement.Update(posUpd);
            return myDbContext.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDbContext.Sys_PositionManagement.FindAsync(id);
            // 更新实体的删除状态
            entity.P_IsDelete = isDeleted;
            // 标记实体为已修改状态
            myDbContext.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDbContext.SaveChangesAsync();
        }
    }
}
